export const urlToBufferXor = (url) => {
    return new Promise(function(resolve, reject) {
        const xhr = new XMLHttpRequest()
        xhr.open('GET', url, true)
        xhr.responseType = 'arraybuffer'
        xhr.onload = function() {
            if (xhr.readyState === 4) {
                if (xhr.status === 200) {
                    const getKey = url.split(/(\.p\.)/g)[0].replace(/.+?\//g, '')
                    const key = getKey
                    const data = new Uint8Array(xhr.response)
                    const encoder = new TextEncoder()
                    const keyArray = encoder.encode(key)
                    const decryptedData = new Uint8Array(data.length)
                    for (let i = 0; i < data.length; i++) decryptedData[i] = data[i] ^ keyArray[i % keyArray.length]
                    let binary = ''
                    const bytes = decryptedData
                    const len = bytes.byteLength
                    for (let i = 0; i < len; i++) binary += String.fromCharCode(bytes[i])
                    const base64 = window.btoa(binary)
                    resolve(`data:image/webp;base64,${base64}`)
                }
            }
        }
        xhr.send()
    })
}